2025-04-08
Cox models for time-to-event data
coxph()cph() from rms to fit three different Cox modelsThis data set is originally from Rossi et al. (1980). The data pertain to 432 convicts who were released from Maryland state prisons in the 1970s and who were followed up for one year after release. Half the released convicts were assigned at random to an experimental treatment in which they were given financial aid; half did not receive aid. Details on Rossi data variables and descriptions are provided at this link.
s_id) included in the rossi.csv file.rossi_432| Variable | Description |
|---|---|
s_id |
Subject ID code |
week |
week of first arrest after release or censoring; all censored observations are censored at 52 weeks. |
arrest |
1 if arrested, 0 if not |
fin |
financial aid: no or yes (main treatment of study) |
age |
age in years at time of release |
race |
two categories: black or other |
wexp |
full-time work experience before incarceration: no or yes |
prio |
number of convictions prior to current incarceration |
rossi_432The rossi.csv file on our 432 data page includes 432 observations on 63 variables, but we’ll look at just the 8 we listed on the last slide.
rossi_432 tibble# A tibble: 432 × 8
s_id week arrest fin age race wexp prio
<chr> <dbl> <dbl> <fct> <dbl> <fct> <fct> <dbl>
1 S-001 20 1 no 27 black no 3
2 S-002 17 1 no 18 black no 8
3 S-003 25 1 no 19 other yes 13
4 S-004 52 0 yes 23 black yes 1
5 S-005 52 0 no 19 other yes 3
6 S-006 52 0 no 24 black yes 2
7 S-007 23 1 no 25 black yes 0
8 S-008 52 0 yes 21 black yes 4
9 S-009 52 0 no 22 black no 6
10 S-010 52 0 no 20 black yes 0
# ℹ 422 more rows
All subjects were followed either for 52 weeks after their release, or until they were (re-)arrested. So all “censored” subjects will have 52 weeks without re-arrest.
fin onlyCall:
coxph(formula = S ~ fin, data = rossi_432)
coef exp(coef) se(coef) z p
finyes -0.3691 0.6914 0.1897 -1.945 0.0517
Likelihood ratio test=3.84 on 1 df, p=0.05013
n= 432, number of events= 114
fit1 model parametersfit1 summaries# Indices of model performance
AIC | AICc | BIC | Nagelkerke's R2 | RMSE | Sigma
----------------------------------------------------------------
1348.924 | 1348.934 | 1352.993 | 0.009 | 0.513 | 0.000
| n | nevent | statistic.log | p.value.log | statistic.sc | p.value.sc | statistic.wald | p.value.wald | statistic.robust | p.value.robust | r.squared | r.squared.max | concordance | std.error.concordance | logLik | AIC | BIC | nobs |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 432.000 | 114.000 | 3.837 | 0.050 | 3.827 | 0.050 | 3.780 | 0.052 | NA | NA | 0.009 | 0.956 | 0.546 | 0.023 | −673.462 | 1,348.924 | 1,351.660 | 114.000 |
fit1fit2)Call:
coxph(formula = S ~ fin + age + race + wexp + prio, data = rossi_432)
coef exp(coef) se(coef) z p
finyes -0.36572 0.69369 0.19074 -1.917 0.05518
age -0.05991 0.94185 0.02199 -2.724 0.00645
raceother -0.34582 0.70764 0.30736 -1.125 0.26053
wexpyes -0.20788 0.81230 0.20905 -0.994 0.32003
prio 0.09212 1.09649 0.02831 3.254 0.00114
Likelihood ratio test=31.58 on 5 df, p=7.211e-06
n= 432, number of events= 114
fit2 model parametersParameter | Coefficient | SE | 90% CI | z | p
-----------------------------------------------------------------
finyes | 0.694 | 0.132 | [0.507, 0.949] | -1.917 | 0.055
age | 0.942 | 0.021 | [0.908, 0.977] | -2.724 | 0.006
raceother | 0.708 | 0.217 | [0.427, 1.173] | -1.125 | 0.261
wexpyes | 0.812 | 0.170 | [0.576, 1.146] | -0.994 | 0.320
prio | 1.096 | 0.031 | [1.047, 1.149] | 3.254 | 0.001
fin hazard ratio estimate of 0.694?prio hazard ratio estimate of 1.096?fit2 summaries# Indices of model performance
AIC | AICc | BIC | Nagelkerke's R2 | RMSE | Sigma
----------------------------------------------------------------
1329.186 | 1329.327 | 1349.528 | 0.074 | 0.514 | 0.000
| n | nevent | statistic.log | p.value.log | statistic.sc | p.value.sc | statistic.wald | p.value.wald | statistic.robust | p.value.robust | r.squared | r.squared.max | concordance | std.error.concordance | logLik | AIC | BIC | nobs |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 432.000 | 114.000 | 31.575 | 0.000 | 31.994 | 0.000 | 30.720 | 0.000 | NA | NA | 0.070 | 0.956 | 0.639 | 0.027 | −659.593 | 1,329.186 | 1,342.867 | 114.000 |
cph() from the rms packagefit1_cph resultsCox Proportional Hazards Model
cph(formula = S ~ fin, data = rossi_432, x = TRUE, y = TRUE,
surv = TRUE)
Model Tests Discrimination
Indexes
Obs 432 LR chi2 3.84 R2 0.009
Events 114 d.f. 1 R2(1,432)0.007
Center -0.1845 Pr(> chi2) 0.0501 R2(1,114)0.025
Score chi2 3.83 Dxy 0.091
Pr(> chi2) 0.0504
Coef S.E. Wald Z Pr(>|Z|)
fin=yes -0.3691 0.1897 -1.95 0.0517
fit1_cph Effects Plotfit1_cph summaries index.orig training test optimism index.corrected n
Dxy 0.0915 0.0889 0.0872 0.0017 0.0898 300
R2 0.0092 0.0108 0.0092 0.0016 0.0077 300
Slope 1.0000 1.0000 1.8983 -0.8983 1.8983 300
D 0.0021 0.0026 0.0021 0.0005 0.0016 300
U -0.0015 -0.0015 0.0007 -0.0022 0.0007 300
Q 0.0036 0.0041 0.0014 0.0027 0.0009 300
g 0.1850 0.1804 0.1850 -0.0046 0.1896 300
survplot() from rms fit1_cphfit1_cph Prediction Plot (1/3)fit1_cph Prediction Plot (2/3)fit1_cph Prediction Plot (3/3)fit1_cph Nomogramfit1_cph Nomogramfit2_cph resultsCox Proportional Hazards Model
cph(formula = S ~ fin + age + race + wexp + prio, data = rossi_432,
x = TRUE, y = TRUE, surv = TRUE)
Model Tests Discrimination
Indexes
Obs 432 LR chi2 31.58 R2 0.074
Events 114 d.f. 5 R2(5,432)0.060
Center -1.5427 Pr(> chi2) 0.0000 R2(5,114)0.208
Score chi2 31.99 Dxy 0.277
Pr(> chi2) 0.0000
Coef S.E. Wald Z Pr(>|Z|)
fin=yes -0.3657 0.1907 -1.92 0.0552
age -0.0599 0.0220 -2.72 0.0065
race=other -0.3458 0.3074 -1.13 0.2605
wexp=yes -0.2079 0.2091 -0.99 0.3200
prio 0.0921 0.0283 3.26 0.0011
fit2_cph Effects Plotfit2_cph Effects Table Effects Response : S
Factor Low High Diff. Effect S.E. Lower 0.95 Upper 0.95
age 20 27 7 -0.41932 0.153950 -0.72105 -0.1175800
Hazard Ratio 20 27 7 0.65750 NA 0.48624 0.8890700
prio 1 4 3 0.27642 0.084911 0.10999 0.4428400
Hazard Ratio 1 4 3 1.31840 NA 1.11630 1.5571000
fin - yes:no 1 2 NA -0.36571 0.190740 -0.73955 0.0081226
Hazard Ratio 1 2 NA 0.69370 NA 0.47733 1.0082000
race - other:black 1 2 NA -0.34585 0.307360 -0.94825 0.2565600
Hazard Ratio 1 2 NA 0.70762 NA 0.38742 1.2925000
wexp - no:yes 2 1 NA 0.20790 0.209060 -0.20184 0.6176400
Hazard Ratio 2 1 NA 1.23110 NA 0.81723 1.8546000
survplot() from rms fit2_cphfin effectsurvplot() from rms fit2_cphrace effectsurvplot() from rms fit2_cphage effectsurvplot() from rms fit2_cphwexp effectsurvplot() from rms fit2_cphprio effectfit2_cph Prediction Plots (1/3)fit2_cph Prediction Plots (2/3)fit2_cph Prediction Plots (3/3)fit2_cph summaries index.orig training test optimism index.corrected n
Dxy 0.2774 0.3002 0.2657 0.0345 0.2429 300
R2 0.0737 0.0851 0.0652 0.0199 0.0538 300
Slope 1.0000 1.0000 0.8892 0.1108 0.8892 300
D 0.0226 0.0265 0.0199 0.0067 0.0159 300
U -0.0015 -0.0015 0.0012 -0.0027 0.0012 300
Q 0.0241 0.0280 0.0187 0.0094 0.0147 300
g 0.6301 0.6808 0.5889 0.0919 0.5382 300
fit2_cph Nomogramfit2_cph Nomogramweek rather than S here…fit3_cphage, and the interaction between prio and wexp.fit3_cphCox Proportional Hazards Model
cph(formula = S ~ rcs(age, 4) + prio * wexp + fin + race, data = rossi_432,
x = T, y = T, surv = T)
Model Tests Discrimination
Indexes
Obs 432 LR chi2 35.99 R2 0.084
Events 114 d.f. 8 R2(8,432)0.063
Center -5.4645 Pr(> chi2) 0.0000 R2(8,114)0.218
Score chi2 41.91 Dxy 0.298
Pr(> chi2) 0.0000
Coef S.E. Wald Z Pr(>|Z|)
age -0.2617 0.1074 -2.44 0.0148
age' 1.7834 1.2207 1.46 0.1440
age'' -3.1846 2.3664 -1.35 0.1784
prio 0.0985 0.0331 2.98 0.0029
wexp=yes -0.1092 0.3097 -0.35 0.7245
fin=yes -0.3441 0.1920 -1.79 0.0730
race=other -0.3828 0.3105 -1.23 0.2176
prio * wexp=yes -0.0100 0.0663 -0.15 0.8806
fit3_cph Effects Plotfit3_cph Effects Table Effects Response : S
Factor Low High Diff. Effect S.E. Lower 0.95 Upper 0.95
age 20 27 7 -0.45052 0.27623 -0.991920 0.090883
Hazard Ratio 20 27 7 0.63730 NA 0.370870 1.095100
prio 1 4 3 0.26574 0.17154 -0.070485 0.601960
Hazard Ratio 1 4 3 1.30440 NA 0.931940 1.825700
wexp - no:yes 2 1 NA 0.12909 0.23346 -0.328480 0.586660
Hazard Ratio 2 1 NA 1.13780 NA 0.720010 1.798000
fin - yes:no 1 2 NA -0.34414 0.19198 -0.720420 0.032138
Hazard Ratio 1 2 NA 0.70883 NA 0.486550 1.032700
race - other:black 1 2 NA -0.38281 0.31051 -0.991400 0.225770
Hazard Ratio 1 2 NA 0.68194 NA 0.371060 1.253300
Adjusted to: prio=2 wexp=yes
survplot() from rms fit3_cphfin effectsurvplot() from rms fit3_cphage effectfit3_cph Prediction Plots (raw)fit3_cph Prediction Plots (plogis)fit3_cph summaries index.orig training test optimism index.corrected n
Dxy 0.2981 0.3269 0.2706 0.0564 0.2417 300
R2 0.0836 0.1016 0.0690 0.0326 0.0510 300
Slope 1.0000 1.0000 0.8319 0.1681 0.8319 300
D 0.0259 0.0321 0.0211 0.0110 0.0149 300
U -0.0015 -0.0015 0.0019 -0.0034 0.0019 300
Q 0.0274 0.0336 0.0192 0.0144 0.0130 300
g 0.6038 0.6922 0.5651 0.1271 0.4766 300
fit3_cph Nomogram432 Class 23 | 2025-04-08 | https://thomaselove.github.io/432-2025/
fit3_cph Nomogram